//
//  YIem_Search_ViewController.m
//  B段_开眼
//
//  Created by YIem on 16/4/26.
//  Copyright © 2016年 Howie. All rights reserved.
//

#import "YIem_Search_ViewController.h"

@interface YIem_Search_ViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *searchArr;
@property (nonatomic, retain) UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSString *str;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, assign) BOOL isUpState;
@property (nonatomic, strong) NSString *url;

@end

@implementation YIem_Search_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.searchArr = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
    self.searchBar.keyboardType = UIKeyboardAppearanceDefault;
    self.searchBar.placeholder = @"输入搜索内容!";
    self.searchBar.delegate = self;
    self.searchBar.barTintColor = [UIColor orangeColor];
    self.searchBar.searchBarStyle = UISearchBarStyleDefault;
    self.navigationItem.titleView = self.searchBar;
   
    
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 40)];
    
    [self.view addSubview:vi];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.text = @"请输入视频标题或描述中的关键词进行搜索";
    self.label.font = [UIFont systemFontOfSize:12];
    [vi addSubview:self.label];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, vi.frame.origin.y + vi.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - 104) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[YIem_FindDetail_Time_TableViewCell class] forCellReuseIdentifier:@"FindDetail_Time_TableViewCell"];
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _isUpState = NO;
        if (![NSStringFromClass([self.url class]) isEqualToString:@"NSNull"]) {
             [self dataBaseSearch];
        }else {
            [_tableView.mj_footer endRefreshing];
        }
      
    }];
    // 返回
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
    

    
    
}




- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    return YES;// 设置为NO 无法回收键盘
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    

    if ([searchText  isEqual: @""]) {
        
        
        
        
    } else {
        // 调用 搜索 数据
        // 刷新 TableView;
        [self.searchArr removeAllObjects];
        self.url = [NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v1/search?&start=0&num=20&query=%@", searchText];
        self.url = [self.url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        self.str = searchText;
        [self dataBaseSearch];
        [self.tableView reloadData];
        
        
        
    }
    


}
// 键盘 上的Search
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
//    NSLog(@"键盘搜索");
    
    if ([searchBar.text  isEqual: @""]) {
        
    } else {
        [self.searchArr removeAllObjects];
        self.url = [NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v1/search?&start=0&num=20&query=%@", searchBar.text];
        self.url = [self.url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        self.str = searchBar.text;
        [self dataBaseSearch];
        [self.tableView reloadData];
        [searchBar setShowsCancelButton:NO animated:YES];
        [searchBar resignFirstResponder];
        
    }
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.searchArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YIem_FindDetail_Time_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FindDetail_Time_TableViewCell" forIndexPath:indexPath];
    
     cell.model = self.searchArr[indexPath.row];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return hEIGHT;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.searchBar resignFirstResponder];
}


- (void)dataBaseSearch
{
//    NSLog(@"-------%@", self.url);
    
    
    [YIem_FindDetailViewModel getDataWithPage:self.url andUpdate:YES andBlock:^(NSMutableArray *dataArr, NSString *str, NSString *total) {
        
        
        for (EveryCellModel *model in dataArr) {
            [self.searchArr addObject:model];
        }
            self.label.font = [UIFont systemFontOfSize:20];
        self.label.text = [NSString stringWithFormat:@"●%@●搜索结果共%@个 ", self.str, total];
        self.url = str;
//        NSLog(@"121211%@", str);
//        NSLog(@"-- %@", dataArr);
        [_tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    }];
}
- (void)rightAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
